﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace M3U8下载神器
{
    public partial class MainForm : Form
    {
        private bool _protectedDownload{set;get;}=false;
        public MainForm()
        {
            InitializeComponent();
            FormClosing += (o, e) =>
            {
               if( MessageBox.Show("确定关闭所有并退出?","选择", MessageBoxButtons.OKCancel)!= DialogResult.OK)
                {
                    e.Cancel=true;
                }
            };
            FormClosed += (o, e) =>
            {
                User.Default.Save();
            };
            Program.Broadcast.Add((o,t,code,msg)=> 
            { 
                if("download_log"==t+"")
                {
                    File.AppendAllText("download.log", msg+"");
                }
            });
            自动下载AToolStripMenuItem_Click(this, null);
        }

        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var newTaskDialog = new NewTaskDialog();
            if (newTaskDialog.ShowDialog() == DialogResult.OK&& newTaskDialog.Package.Begin())
            {
                var form = new DownloadForm();
                form.Build(newTaskDialog.Package);
                form.Show();
                form.Start();
            }
        }

        private void 导入IToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter= "M3u8List|*.m3ul";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                M3U8Package package = M3U8Package.Import(ofd.FileName);
                if (package.Begin())
                {
                    var form = new DownloadForm();
                    form.Build(package);
                    form.Show();
                    form.Start();
                }
            }
        }

        private void 批量下载BToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form form in Application.OpenForms)
            {
                if(form is BatchDownloadForm)
                {
                    foreach (Form form2 in Application.OpenForms)
                    {
                        form2.Show();
                    }
                    _protectedDownload = false;
                    return;
                }
            }
            new BatchDownloadForm().Show();
        }

        private void 动作AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _protectedDownload = !_protectedDownload;
            foreach (Form form in Application.OpenForms)
            {
                Debug.WriteLine(form.ToString()+(form==this));
                if (form != this)
                {
                    if (_protectedDownload)
                    {
                        form.Hide();
                    }
                    else
                    {
                        form.Show();
                    }
                }
            }
        }

        private void tS合并CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "ts文件|*.ts";
            ofd.Multiselect = true;
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter= "ts文件|*.ts"; 

            if (ofd.ShowDialog() == DialogResult.OK&&sfd.ShowDialog()==DialogResult.OK)
            {
                using (var output = File.Create(sfd.FileName))
                {
                    for (int i = 0; i < ofd.FileNames.Length; i++)
                    {
                        var file = ofd.FileNames[i];
                        using (var ts = File.OpenRead(file))
                        {
                            ts.CopyTo(output);
                        }
                    }
                }
            }
        }

        private void 自动下载AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for(int i = 0; i < Application.OpenForms.Count; i++)
            {
                if(Application.OpenForms[i] is PipeForm)
                {
                    return;
                }
            }
            var pf = new PipeForm();
            pf.Show();
            pf.Visible = false;
            
            自动下载AToolStripMenuItem.Enabled = false;
        }
    }
}
